package org.elanfox.intellicommunity.biz.service.core.equipment.impl;

import org.elanfox.intellicommunity.biz.dto.equipment.LocatorAreaSaveDTO;
import org.elanfox.intellicommunity.biz.entity.equipment.LocatorAreaPO;
import org.elanfox.intellicommunity.biz.repository.equipment.LocatorAreaRepository;
import org.elanfox.intellicommunity.biz.service.core.equipment.LocatorAreaService;
import org.elanfox.intellicommunity.biz.vo.LocatorAreaVO;
import org.elanfox.intellicommunity.data.base.base.AbstractCrudService;
import org.elanfox.intellicommunity.data.base.dto.BaseSaveDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author allan
 * @version 1.0.0
 * @date 2/19/21
 */
@Service
public class LocatorAreaServiceImpl extends AbstractCrudService<LocatorAreaPO, Integer> implements LocatorAreaService {
    @Autowired
    protected LocatorAreaServiceImpl(LocatorAreaRepository repository) {
        super(repository);
    }

    @Override
    public LocatorAreaPO save(BaseSaveDTO saveDTO) {
        LocatorAreaSaveDTO dto = (LocatorAreaSaveDTO) saveDTO;
        LocatorAreaPO po;
        if (dto.getId() > 0) {
            po = findById(dto.getId());
        } else {
            po = new LocatorAreaPO();
        }

        po.setName(dto.getName());
        if (dto.getClosest() > 0) {
            po.setClosest(new LocatorAreaPO(dto.getClosest()));
        }
        if (dto.getMiddle() > 0) {
            po.setMiddle(new LocatorAreaPO(dto.getMiddle()));
        }
        if (dto.getFaraway() > 0) {
            po.setFaraway(new LocatorAreaPO(dto.getFaraway()));
        }

        return save(po);
    }

    @Override
    public List<LocatorAreaVO> findAllArea() {
        List<LocatorAreaPO> pos = findAll();

        return pos.stream().map(LocatorAreaVO::new).collect(Collectors.toList());
    }
}
